package com.rocpenghua.util;

/**
 * @author pengp
 * @date 2024年03月07日 18:00
 */
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JSONUtils {
    private static final ObjectMapper MAPPER = new ObjectMapper();
    public static <T> List<T> jsonToList(String jsonText,Class<T> clazz) {
        try {
            return MAPPER.readValue(jsonText, new TypeReference<List<T>>() {});
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }
    public static <T> String listToJson(List<T> list,Class<T> clazz){
        try {
            return MAPPER.writeValueAsString(list);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static Map jsonToMap(String jsonText){
        try {
            return MAPPER.readValue(jsonText, Map.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return new HashMap();
    }
    public static Map jsonToMapArr(String jsonText,String key){
        try {
            Map map = MAPPER.readValue(jsonText, Map.class);
            String[] keys = key.split(",");
            for (String k:keys){
                map.put(k,((List<?>)map.get(k)).toArray(new String[((List<?>)map.get(k)).size()]));
            }
            return map;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return new HashMap();
    }

    public static String mapToJson(Map map){
        try {
            return MAPPER.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "";
    }

    @SuppressWarnings("unchecked")
    public static <T> T jsonToObject(String jsonText,Class<T> clazz) {
        try {
            return MAPPER.readValue(jsonText, clazz);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static <T> String objectToJson(T t,Class<T> clazz){
        try {
            return MAPPER.writeValueAsString(t);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static Object getMapValueByKey(Map<String,Object> param,String str){
        return param.get(str) == null?"":param.get(str);
    }

    public static String getMapStrValueByKey(Map<String,Object> param,String str){
        return param.get(str) == null?"": String.valueOf(param.get(str));
    }

    public static String objectToJson(Object arg) {
        try {
            return MAPPER.writeValueAsString(arg);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
}

